package com.example.nettychattingroomandroid;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.nettychattingroomandroid.client.ChatClient;
import com.example.nettychattingroomandroid.entity.ChatMessage;


import java.io.IOException;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public static String username;
    public static final int SHOW_MSG=1;
    private Button connectbtn;
    private Button sendbtn;
    private TextView messageview;
    private EditText editText;
    private EditText editsender;
    private EditText editreceiver;
    private ChatClient client;
    private static MainActivity mainActivity;
    public MainActivity(){
        mainActivity=this;
    }

    public static MainActivity getMainActivity() {
        return mainActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectbtn=(Button)findViewById(R.id.connectbtn);
        sendbtn=(Button)findViewById(R.id.sendbtn);
        messageview=(TextView)findViewById(R.id.messageview);
        editText=(EditText)findViewById(R.id.editmessage);
        editsender=(EditText)findViewById(R.id.editsender);
        editreceiver=(EditText)findViewById(R.id.editreceiver);
        connectbtn.setOnClickListener(this);
        sendbtn.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.connectbtn){
            System.out.println("连接服务器！");
//            Toast.makeText(MainActivity.this, "连接服务器！", Toast.LENGTH_SHORT).show();
            username=editsender.getText().toString();
            new Thread(){
                @Override
                public void run() {
                    connect();
                }
            }.start();

        }
        if(v.getId()==R.id.sendbtn){
            System.out.println("发送消息："+editText.getText().toString());
//            Toast.makeText(MainActivity.this, "发送消息："+editText.getText().toString(), Toast.LENGTH_SHORT).show();
            ChatMessage msg=new ChatMessage(editsender.getText().toString(), editreceiver.getText().toString(),editText.getText().toString() ,2);
            send(msg);
        }
    }
    public void connect(){
        try {
            client=new ChatClient("10.1.89.218",8888);
            client.start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void send(ChatMessage cmsg){
        client.sendMsg(cmsg);
    }

    private Handler msghandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==SHOW_MSG){
                messageview.setText(messageview.getText().toString()+msg.getData().getString("msg")+"\n");
            }
        }
    };

    public Handler getMsghandler() {
        return msghandler;
    }
}
